<?php

/*
 * by : Praditya Kurniawan
 * web : http://masiyak.com
 * email : aku@masiyak.com
 */

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class umur {

    function hitung_umur($time1, $time2, $precision = 6) {
        // If not numeric then convert texts to unix timestamps
        if (!is_int($time1)) {
            $time1 = strtotime($time1);
        }
        if (!is_int($time2)) {
            $time2 = strtotime($time2);
        }

        // If time1 is bigger than time2
        // Then swap time1 and time2
        if ($time1 > $time2) {
            $ttime = $time1;
            $time1 = $time2;
            $time2 = $ttime;
        }

        // Set up intervals and diffs arrays
        //jangan di ganti jadi bahasa indonesia soalnya berpengaruh ke algoritma
        $intervals = array('year', 'month', 'day', 'hour', 'minute', 'second');
        $waktu_indo = array('Tahun', 'Bulan', 'Hari', 'Jam', 'Menit', 'Detik');
        $diffs = array();

        // Loop thru all intervals
        //set penunjuk untuk menentukan letak waktu
        $penunjuk = 0;
        foreach ($intervals as $interval) {
            // Set default diff to 0
            $diffs[$waktu_indo[$penunjuk]] = 0;
            // Create temp time from time1 and interval
            $ttime = strtotime("+1 " . $interval, $time1);
            // Loop until temp time is smaller than time2
            while ($time2 >= $ttime) {
                $time1 = $ttime;
                $diffs[$waktu_indo[$penunjuk]]++;
                // Create new temp time from time1 and interval
                $ttime = strtotime("+1 " . $interval, $time1);
            }
            $penunjuk++;
        }

        $count = 0;
        $times = array();
        // Loop thru all diffs
        foreach ($diffs as $interval => $value) {
            // Break if we have needed precission
            if ($count >= $precision) {
                break;
            }
            // Add value and interval 
            // if value is bigger than 0
            if ($value > 0) {
// Add s if value is not 1
//                if ($value != 1) {
//                    $interval .= "s";
//                }
// Add value and interval to times array
                $times[] = $value . " " . $interval;
                $count++;
            }
        }

        // Return string with times
        return implode(", ", $times);
    }

}

?>
